package com.xinan.tool.aoi.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.entity.UrlEncodedFormEntity;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.*;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.io.entity.StringEntity;
import org.apache.hc.core5.http.message.BasicNameValuePair;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author mxl
 */
@Slf4j
public class HttpClientUtil {
    public static String httpClientPost(String url, String paramStr, List<Header> headers, String requestText, ContentType contentType, String charset) {
        String result = null;
        HttpPost httpPost = new HttpPost(url);
        if (headers != null) {
            // 请求头参数
            for (Header header : headers) {
                httpPost.addHeader(header);
//                postMethod.addRequestHeader(header);
            }
        }

        if (paramStr != null) {
            List<NameValuePair> nvps = createParamsFromStr(paramStr);
            httpPost.setEntity(new UrlEncodedFormEntity(nvps));
        }
        if (StringUtils.isNotEmpty(requestText)) {
            httpPost.setEntity(new StringEntity(requestText, contentType, charset, false));
        }

        try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
            try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
                // 获取响应信息
                result = EntityUtils.toString(response.getEntity());
            }
        } catch (IOException | ParseException e) {
            log.error("", e);
        }
        return result;
    }

    /**
     * httpClientGet(get方式请求接口)
     *
     * @param url
     * @return
     * @throws Exception
     * @Title: httpClientGet
     * @Description: get方式请求接口
     */
    public static String httpClientGet(String url) {
        String resultContent = null;
        HttpGet httpGet = new HttpGet(url);
        try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
            try (CloseableHttpResponse response = httpclient.execute(httpGet)) {
                // 获取状态码
                System.out.println(response.getVersion()); // HTTP/1.1
                System.out.println(response.getCode()); // 200
                System.out.println(response.getReasonPhrase()); // OK
                HttpEntity entity = response.getEntity();
                // 获取响应信息
                resultContent = EntityUtils.toString(entity);
            }
        } catch (IOException | ParseException e) {
            e.printStackTrace();
        }
        return resultContent;
    }

    private static List<NameValuePair> createParamsFromStr(String paramStr) {
        List<NameValuePair> results = new ArrayList<>();
        if (null != paramStr) {
            String[] paires = paramStr.split("&");
            int lenth = paires.length;
//            results = new NameValuePair[lenth];
            for (int i = 0; i < lenth; i++) {
                String str = paires[i];
                String[] pair = str.split("=");
                NameValuePair nvp = new BasicNameValuePair(pair[0], pair[1]);
//                nvp.setName(pair[0]);
//                nvp.setValue(pair[1]);
                results.add(nvp);
            }
        }
        return results;
    }
}

